﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PyroGen;


namespace PyroGenTest
{


    [TestClass]
    public class PositionFinderTests
    {
        [TestMethod]
        public void PoistionConstructors()
        {
            Position posa = new Position(1, CodeType.Code, Terminator.Start);
            Position posb = new Position(1, 0, CodeType.Code, Terminator.Start);

            Position posc = new Position(1, 0, new Tuple<CodeType, Terminator>(CodeType.Code
                , Terminator.Start));

            Assert.AreEqual<Position>(posc, posa);
            Assert.AreEqual<Position>(posa, posb);

        }

        [TestMethod]
        public void PositionAccesors()
        {
            Position posx = new Position(3, 5, CodeType.ConfigCode, Terminator.End);
            Assert.AreEqual<int>(posx.Length, 5);
            Assert.AreEqual<int>(posx.Location, 3);
            Assert.AreEqual<int>(posx.Start, 3);
            Assert.AreEqual<int>(posx.End, 8);
        }

        [TestMethod]
        public void PositionOverlapingFalse()
        {
            Position posa = new Position(0, 2, new Tuple<CodeType,Terminator>(CodeType.ScriptCode, Terminator.Start));
            Position posb = new Position(2, 2, new Tuple<CodeType,Terminator>(CodeType.ScriptCode, Terminator.Start));

            Assert.AreEqual(false, posa.Overlaps(posb));
            Assert.AreEqual(false, posb.Overlaps(posa));
        }

        [TestMethod]
        public void PositionOverlapingSimpleTrue()
        {
            Position posa = new Position(0, 3, new Tuple<CodeType,Terminator>(CodeType.ScriptCode,Terminator.Start));
            Position posb = new Position(2, 2, new Tuple<CodeType,Terminator>(CodeType.ScriptCode,Terminator.Start));

            Assert.AreEqual(true, posa.Overlaps(posb));
            Assert.AreEqual(true, posb.Overlaps(posa));
        }



        [TestMethod]
        public void PositionFinderSimpleMatching()
        {
            PositionFinder posFinder = new PositionFinder("bacbacbac");
            Dictionary<string, Tuple<CodeType, Terminator>> dict = new Dictionary<string, Tuple<CodeType, Terminator>>();
            string StartString = "cb";
            string EndString = "a";
            int stlen = StartString.Length;
            int enlen = EndString.Length;
            dict.Add(StartString, new Tuple<CodeType, Terminator>(CodeType.Expression, Terminator.Start));
            dict.Add(EndString, new Tuple<CodeType, Terminator>(CodeType.Expression, Terminator.End));

            posFinder.AddAllPositions(dict);


            Assert.AreEqual<int>(posFinder.BorderPositions.Count, 7);

            Assert.AreEqual<Position>(posFinder.BorderPositions[0], new Position(0, 0, CodeType.Text, Terminator.Start));
            Assert.AreEqual<Position>(posFinder.BorderPositions[1], new Position(1, enlen, CodeType.Expression, Terminator.End));
            Assert.AreEqual<Position>(posFinder.BorderPositions[2], new Position(2, stlen, CodeType.Expression, Terminator.Start));
            Assert.AreEqual<Position>(posFinder.BorderPositions[3], new Position(4, enlen, CodeType.Expression, Terminator.End));
            Assert.AreEqual<Position>(posFinder.BorderPositions[4], new Position(5, stlen, CodeType.Expression, Terminator.Start));
            Assert.AreEqual<Position>(posFinder.BorderPositions[5], new Position(7, enlen, CodeType.Expression, Terminator.End));
            Assert.AreEqual<Position>(posFinder.BorderPositions[6], new Position(9, CodeType.Text, Terminator.End));
        }



    }


}


